<?php
require_once SOURCE_EXT_PATH . 'common/ModelLib.class.php';
class FinanceDailyClass extends E_finance_daily
{
	public $Message;
}

class FinanceLib
{
	private $dailyModel;
	function __construct()
	{
		require_once APP_MODEL_PATH . 'e_finance_daily.php';
		$this->dailyModel = new FinanceDailyModel ();
	}
	public function getDailyList()
	{
		$where = '';
		$url = siteUrl ( 'finance/dailylist' );
		if (isset ( $_GET ['keywords'] ))
		{
			$info = json_decode ( urldecode ( $_GET ['keywords'] ) );
			if (is_object ( $info ))
			{
				$where = 'where 1=1';
				$where .= (isset ( $info->startTime ) && $info->startTime) ? " and CreateTime >='" . $info->startTime . " 00:00:00'" : '';
				$where .= (isset ( $info->endTime ) && $info->endTime) ? " and CreateTime <='" . $info->endTime . " 23:59:59'" : '';
				$where .= isset ( $info->Balance ) && $info->Balance ? " and Balance ='" . $info->Balance . "'" : '';
				$where .= isset ( $info->Description ) && $info->Description ? " and Description like'%" . $info->Description . "%'" : '';
				$url = siteUrl ( 'finance/dailylist' ) . '?keywords=' . urlencode ( $_GET ['keywords'] );
			}
		}
		if ($_POST)
		{
			$info = $_POST;
			$where = 'where 1=1';
			$where .= isset ( $info ['startTime'] ) && $info ['startTime'] ? " and CreateTime >='" . $info ['startTime'] . " 00:00:00'" : '';
			$where .= isset ( $info ['endTime'] ) && $info ['endTime'] ? " and CreateTime <='" . $info ['endTime'] . " 23:59:59'" : '';
			$where .= isset ( $info ['Balance'] ) && $info ['Balance']? " and Balance ='" . $info ['Balance'] . "'" : '';
			$where .= isset ( $info ['Description'] )&& $info ['Description'] ? " and Description like'%" . $info ['Description'] . "%'" : '';
			$urlKeywords = urlencode ( json_encode ( $info ) );
			$url = siteUrl ( 'finance/dailylist' ) . '?keywords=' . $urlKeywords;
		}
		require_once SOURCE_EXT_PATH . 'library/PageLib.php';
		$total = $this->dailyModel->getCount ( $where )->sum;
		$start = isset ( $_GET ['pre_page'] )&&$_GET ['pre_page'] ? ($_GET ['pre_page'] - 1) * PAGE_SIZE : 0;
		ViewData::$form_data->list = $this->dailyModel->getList ( $where, $start, PAGE_SIZE );
		$pageArray = array ('total' => $total, 'perpage' => PAGE_SIZE, 'url' => $url );
		$page = new PageLib ( $pageArray );
		ViewData::$form_data->page = $page->show ();
	}
	public function addDailyForm()
	{
		ViewData::$form_data = BaseAction::parseForm ( "POST", ViewData::$form_data );
		ViewData::$form_hint->InMoney = FormLib::getCost ( 'InMoney' ,'POST');
		ViewData::$form_hint->OutMoney = FormLib::getCost ( 'OutMoney','POST' );
		ViewData::$form_hint->CreateTime = FormLib::getText64 ( 'CreateTime' ,'POST');
		return;
	}
	public function addDailyLogic()
	{
		$info = ViewData::$form_data;
		$adminInfo = AdminSession::getAdminSessionInfo ();
		$info->Balance=$info->InMoney-$info->OutMoney;
		$info->EndTime="0000-00-00 00:00:00";
		$info->StartTime="0000-00-00 00:00:00";
		$info->Operater = $adminInfo ['AdminName'];
		$info->MoneyType = 5;
		$info->StudentId = 0;
		$info->OperationTime = date ( 'Y-m-d H:i:s' );
		return $this->dailyModel->addInfo ( $info );
	}
	/**
	 * 
	 * 提供给fulltime和lotus那边的财务添加
	 * @param unknown_type $info
	 */
	public function addDaily($info)
	{
		$info->CreateTime = date ( 'Y-m-d H:i:s' );
		$adminInfo = AdminSession::getAdminSessionInfo ();
		$info->Operater = $adminInfo ['AdminName'];
		$info->OperationTime = date ( 'Y-m-d H:i:s' );
		return $this->dailyModel->addInfo ( $info );
	}
	
	public function exportExcel($idList)
	{
		$info = array ();
		$info [0] = array ('日期','收入','支出','结存余额','描述','操作人');
		foreach ( $idList as $value )
		{
			$info [] = $this->dailyModel->getExcelById ( $value );
		}
		require SOURCE_EXT_PATH . 'library/ExportExcelLib.php';
		$xls = new ExportExcelLib ();
		$newInfo=$xls->addArray ( $info );
		$xls->generateXML ( '财务日账单列表',$newInfo );
		exit ();
	}
	public function getDailyInfoByFinanceId($financeId)
	{
		return $this->dailyModel->getById ( $financeId );
	}
	public function getDailyInfoByStudentIdAndMoneyType($studentId,$moneyType){
		return $this->dailyModel->getByStudentIdAndMoneyType ( $studentId,$moneyType );
	}
	public function delDailyInfo($financeId)
	{
		return $this->dailyModel->delById ( $financeId );
	}
	public function setDailyInfo()
	{
		$info = new E_finance_daily ();
		$info->InMoney = ViewData::$form_data->InMoney;
		$info->OutMoney = ViewData::$form_data->OutMoney;
		$info->Description = ViewData::$form_data->Description;
		$info->MoneyType = ViewData::$form_data->MoneyType;
		$info->Balance = ViewData::$form_data->Balance;
		$info->CreateTime = ViewData::$form_data->CreateTime.' '.date('H:i:s');
		$info->OperationTime = date ( 'Y-m-d H:i:s' );
		$adminInfo = AdminSession::getAdminSessionInfo ();
		$info->Operater = $adminInfo ['AdminName'];
		return $this->dailyModel->setInfoById ( $info, ViewData::$form_data->FinanceId );
	}
}